package networked;

import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.Network;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.CartoonEdgeFilter;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import de.tsajar.es.EntityDataAppState;
import de.tsajar.es.networking.client.RemoteEntityData;
import de.tsajar.es.render.RenderSystem;
import modelinitializer.CubeInitializer;

/**
 *
 * @author Eike Foede
 */
public class TestArrow extends SimpleApplication implements ActionListener {

    EntityData ed;
    float count = 0;
    Client c;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestArrow tr = new TestArrow();
        tr.start();
    }

    /**
     * A centred plus sign to help the player aim.
     */
    protected void initCrossHairs() {
        setDisplayStatView(false);
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        BitmapText ch = new BitmapText(guiFont, false);
        ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
        ch.setText("+"); // crosshairs
        ch.setLocalTranslation( // center
                settings.getWidth() / 2 - ch.getLineWidth() / 2, settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
        guiNode.attachChild(ch);
    }


    private void initClient(String host, int port) {
        try {
            Helper.initSerializers();
            c = Network.connectToServer(host, port);
            c.start();
            RemoteEntityData red = new RemoteEntityData(c);
            stateManager.attach(new EntityDataAppState(red));
            stateManager.attach(new RenderSystem(new CubeInitializer(assetManager)));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private void addToonEdge() {
        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
        CartoonEdgeFilter toon = new CartoonEdgeFilter();
        toon.setEdgeWidth(0.7f);
        toon.setEdgeIntensity(1.0f);
        toon.setNormalThreshold(0.6f);
        fpp.addFilter(toon);
        viewPort.addProcessor(fpp);
    }

    @Override
    public void simpleInitApp() {
        flyCam.setMoveSpeed(10);
//        addScene();
        initCrossHairs();
//        addToonEdge();
//        initServer(1234);
        System.out.println("bfInit");
        initClient("localhost", 1234);
        System.out.println("afInit");
            inputManager.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
            inputManager.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
            inputManager.addListener(this, "SHOOT");
        
//        stateManager.attach(new ArrowSystem(rootNode));
//        stateManager.attach(new RenderSystem(new CubeInitializer(assetManager)));
//        stateManager.getState(ArrowSystem.class).setGravity(new Vector3f(0, 0, 0));
        ed = stateManager.getState(EntityDataAppState.class).getEd();
//        fireArrow(new Vector3f(-10, 0, 0), new Vector3f(0, 7, -20));

    }

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
        if (isPressed) {
            if (name.equals("SHOOT")) {
                fireArrow(cam.getLocation().clone(), cam.getDirection().normalize().clone());
            }
        }
    }

    private void fireArrow(Vector3f location, Vector3f direction) {
        ShootMessage sm = new ShootMessage(location, direction);
        sm.setReliable(true);
        c.send(sm);
    }
}
